package com.xiaoma.common.extend

import android.content.Context
import android.content.res.Configuration
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment

/**
 * author: liangjingbo
 * date: 2022/6/21
 * describe:
 */

/**
 * 获取状态栏高度
 */
fun Context.getStatusBarHeight(): Int {
    var result = 0
    //获取状态栏高度的资源id
    val resourceId: Int = resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resourceId > 0) {
        result = resources.getDimensionPixelSize(resourceId)
    }
    return result
}

fun Context.getScreenWidth(): Int {
    return this.resources.displayMetrics.widthPixels
}

fun Context.getScreenHeight(): Int {
    return this.resources.displayMetrics.heightPixels
}

fun Context.isSystemDarkMode(newConfig: Configuration? = null): Boolean {
    val uiMode = newConfig?.uiMode ?: resources.configuration.uiMode
    val systemMode = uiMode and Configuration.UI_MODE_NIGHT_MASK
    return systemMode == Configuration.UI_MODE_NIGHT_YES
}

fun Context.getColorRes(@ColorRes id: Int) = ContextCompat.getColor(this, id)

fun Context.getColorStateListRes(@ColorRes id: Int) = ContextCompat.getColorStateList(this, id)

fun Context.getDrawableRes(@DrawableRes id: Int) = ContextCompat.getDrawable(this, id)
